package com.example.springboot.controller;

import com.example.springboot.dao.CovidData;
import com.example.springboot.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    @RequestMapping("/index")
    public String index(Model model) throws IOException {
        //List<CovidData> covidData = dataService.ReadDataFromFile();
        List<CovidData> covidData = dataService.GetData();

        model.addAttribute("data",covidData);
        return "index";

    }

    @RequestMapping("/query")
    public String query(Model model,@RequestParam("country") String country) throws IOException {
        List<CovidData> covidData = dataService.ReadDataFromFile();

        covidData = covidData.stream()
                .filter(list -> list.getCountry().equals(country)
                || list.getState().equals(country))
                .collect(Collectors.toList());

        model.addAttribute("data", covidData);


        return "index";
    }

}
